Skip to content

发送数据 - TcpClientSend

函数简介

发送数据(异步操作)。

接口名称

TcpClientSend

DLL调用

c
int32_t TcpClientSend(int64_t instance, int64_t client_handle, int64_t data, int32_t data_len);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
client_handle长整数型客户端句柄。
data长整数型数据指针。
data_len整数型数据长度。

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000);
    // 连接成功后(回调 event_type=0)再发送
    const char* msg = "hello server";
    ola.TcpClientSend(client, (long)msg, (int)strlen(msg));
    ola.TcpClientDestroy(client);
}
csharp
using OLAPlug;

var ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0)
{
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000);
    byte[] data = System.Text.Encoding.UTF8.GetBytes("hello server");
    ola.TcpClientSend(client, data);
    ola.TcpClientDestroy(client);
}
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
client = ola.TcpClientCreate(None, 0, 1)
if client:
    # enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000)
    msg = b"hello server"
    ola.TcpClientSend(client, msg)
    ola.TcpClientDestroy(client)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000);
    byte[] data = "hello server".getBytes();
    ola.TcpClientSend(client, data);
    ola.TcpClientDestroy(client);
}
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
long client = ola.TcpClientCreate(nil, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientSend(client, []byte("hello server"))
    ola.TcpClientDestroy(client);
}
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
long client = ola.TcpClientCreate(None, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.tcp_client_connect(&client, "127.0.0.1", 9000);
    ola.tcp_client_send(&client, b"hello server");
    ola.TcpClientDestroy(client);
}
cpp
var ola = com("OlaPlug.OlaSoft")
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientSend(client, "hello server")
    ola.TcpClientDestroy(client);
}
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
client = ola.TcpClientCreate(0, 0, 1)
If client <> 0 Then
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientSend(client, "hello server")
    ola.TcpClientDestroy(client)
End If
text
.局部变量 ola, OLAPlug
ola.创建 ()
client = ola.TcpClientCreate (0, 0, 1)
.如果真 (client ≠ 0)
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect (client, "127.0.0.1", 9000)
    ola.TcpClientSend (client, "hello server")
    ola.TcpClientDestroy (client)
.如果真结束
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
long client = ola.TcpClientCreate(null, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientSend(client, "hello server");
    ola.TcpClientDestroy(client);
}
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
long client = ola.TcpClientCreate(0, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000)
    ola.TcpClientSend(client, "hello server")
    ola.TcpClientDestroy(client);
}
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
long client = ola.TcpClientCreate(nullptr, 0, 1);
if (client != 0) {
    // enable_packet_protocol=1 启用分包协议
    ola.TcpClientConnect(client, "127.0.0.1", 9000);
    ola.TcpClientSend(client, (long)"hello server", 12);
    ola.TcpClientDestroy(client);
}

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
    TcpClientConnect(instance, client, "127.0.0.1", 9000);
    const char* msg = "hello server";
    TcpClientSend(instance, client, (long)msg, (int)strlen(msg));
    TcpClientDestroy(instance, client);
}
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long TcpClientCreate(long ola, long callback, long user_data, int enable_packet_protocol);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientDestroy(long ola, long client_handle);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientConnect(long ola, long client_handle, string host, int port);
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern int TcpClientSend(long ola, long client_handle, long data, int data_len);

long instance = CreateCOLAPlugInterFace();
long client = TcpClientCreate(instance, 0, instance, 1);
if (client != 0) {
    TcpClientConnect(instance, client, "127.0.0.1", 9000);
    const char* msg = "hello server";
    TcpClientSend(instance, client, (long)msg, (int)strlen(msg));
    TcpClientDestroy(instance, client);
}
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
client = ola.TcpClientCreate(instance, 0, instance, 1)
if client:
    ola.TcpClientConnect(instance, client, "127.0.0.1", 9000);
    const char* msg = "hello server";
    ola.TcpClientSend(instance, client, (long)msg, (int)strlen(msg));
    ola.TcpClientDestroy(instance, client)

返回值

返回值说明
(返回值)整数型,1 成功(加入发送队列),0 失败。

注意事项

项目说明
这是异步操作这是异步操作,数据会被加入发送队列。
发送完成后会触发回调(event_type=4)发送完成后会触发回调(event_type=4)。
如果启用了分包协议如果启用了分包协议,会自动添加4字节长度前缀。
如果未连接如果未连接,发送会失败。
数据会被复制数据会被复制,调用后可以立即释放原始数据。